<?php
class CV_PDO extends PDO{
	protected $dsn;
	public function __construct( $dsn = NULL , $user = NULL , $password = NULL , $driver_options = NULL ){
		parent::__construct( $dsn, $user, $password, $driver_options );		 
		$this->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
		$this->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC );
		$this->setAttribute( PDO::ATTR_STATEMENT_CLASS, array("CV_PDOStatement") );
	}
	public function fetchAll( $prepared_sql, $args = NULL ) {
		return $this->prepare( $prepared_sql )->execute( $args )->fetchAll();
	}
	public function fetch( $prepared_sql, $args = NULL ) {
		return $this->prepare( $prepared_sql )->execute( $args )->fetch();
	}
	public function markers( $array ){
		return "?".str_repeat(",?",count($array)-1);
	}
}

class CV_PDOStatement extends PDOStatement{
	public function execute( $params = NULL ){
		try{
			if( $params !== NULL ){
				if( !is_array($params) ){
					$params = array($params);
				}
				$params = array_values( CV_Array::flatten( CV_Collection::unwrap_recursive( $params ) ) );
				$result = parent::execute( $params );
			} else {
				$result = parent::execute();
			}
			return $this;
		}catch( PDOException $e ){
			throw new PDOException( $e->getMessage()."\n SQL: ".$this->queryString. "\n Parameter: ".strtr(var_export($params,true),array("\r"=>'',"\n"=>'')) );
		}
	}
}